﻿using System.Collections.Generic;
using System.Linq;
using Apps.Web.Core;
using Apps.IBLL.LMS;
using Apps.Locale;
using System.Web.Mvc;
using Apps.Common;
using Apps.IBLL;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.BLL.SelfRcRelated;
using Apps.Models.CustomModels;
using Apps.BLL.YANX;
using DocumentFormat.OpenXml.Office2010.Excel;
using Apps.BLL.LMS;

namespace Apps.Web.Areas.LMS.Controllers
{
    public class TestDataRelatedController : Controller
    {
        SelfRcRelatedBLL bll = new SelfRcRelatedBLL();
        YANX_RECORD_BLL bllYx = new YANX_RECORD_BLL();
        ValidationErrors errors = new ValidationErrors();
        LMS_TESTDATA_AUDITBLL m_testdataBLL = new LMS_TESTDATA_AUDITBLL();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [SupportFilter(ActionName = "Index")]
        public JsonResult GetList()
        {
            int totalCount = 0;
            List<TestDataOutPutModel> list = m_testdataBLL.GetSugarList(new Models.CustomModels.TestDataQuertParaModel
            {
                BatchCode = Utils.ObjectToStr(Request["BatchCode"]),
                BatchType = Utils.ObjectToStr(Request["BatchType"]),
                EndDate = Utils.ObjectToStr(Request["EndDate"]),
                MaterielName = Utils.ObjectToStr(Request["MaterielName"]),
                PageIndex = Utils.ObjToInt(Request["page"], 1),
                PageSize = Utils.ObjToInt(Request["rows"], 15),
                StartDate = Utils.ObjectToStr(Request["StartDate"]),
                TestCode = Utils.ObjectToStr(Request["TestCode"]),
                TypeStr = Utils.ObjectToStr(Request["TypeStr"]),
                ApprovalStatus = Utils.ObjToInt(Request["ApprovalStatus"], -1),
                ISJSUAN = Utils.ObjectToStr(Request["isJsuan"]),
                ISRELATED = Utils.ObjectToStr(Request["jsRelated"]),
            }, ref totalCount);
            if (list != null)
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        list[i].STR_COLLECT_TIME = list[i].COLLECT_TIME.ToString("yyyy-MM-dd HH:mm:ss");
                        list[i].STR_MATCH_TIME = list[i].MATCH_TIME.ToString("yyyy-MM-dd");
                        //if (list[i].BATCH_TYPE == "入厂煤")
                        //{
                        //    list[i].CSR = list[i].SELF_CSR;
                        //    list[i].CRI = list[i].SELF_CRI;
                        //    if (!string.IsNullOrEmpty(list[i].SELF_YANX_VALUE))
                        //        list[i].YANX_VALUE = list[i].SELF_YANX_VALUE;
                        //    //list[i].YANX_VALUE = list[i].SELF_YANX_VALUE;
                        //}
                        //else
                        //{
                        //    list[i].CSR = string.IsNullOrEmpty(list[i].CSR) ? list[i].SELF_CSR : list[i].CSR;
                        //    list[i].CRI = string.IsNullOrEmpty(list[i].CRI) ? list[i].SELF_CRI : list[i].CRI;
                        //    list[i].YANX_VALUE = string.IsNullOrEmpty(list[i].YANX_VALUE) ? list[i].SELF_YANX_VALUE : list[i].YANX_VALUE;
                        //}
                        //list[i].CRI = Utils.ToOnePoint(list[i].CRI); 
                        //list[i].CSR = Utils.ToOnePoint(list[i].CSR);
                        list[i].BATCH_TYPE = list[i].BATCH_TYPE == "自采煤" ? "其他" : list[i].BATCH_TYPE;
                        list[i].CSR = !string.IsNullOrEmpty(list[i].SELF_CSR) ? Utils.ToOnePoint(list[i].SELF_CSR) : Utils.ToOnePoint(list[i].CSR);
                        list[i].CRI = !string.IsNullOrEmpty(list[i].SELF_CRI) ? Utils.ToOnePoint(list[i].SELF_CRI) : Utils.ToOnePoint(list[i].CRI);
                        list[i].YANX_VALUE = !string.IsNullOrEmpty(list[i].SELF_YANX_VALUE) ? list[i].SELF_YANX_VALUE : list[i].YANX_VALUE;

                        if (!string.IsNullOrEmpty(list[i].XJLM_CODE) || !string.IsNullOrEmpty(list[i].XJLJT_CODE))
                            list[i].SELF_BATCH_CODE = list[i].XJLM_CODE + "," + list[i].XJLJT_CODE;
                    }
                    ViewBag.ID = list[0].AUDIT_ID;
                }
            return Json(new Pagination.PaginationResult<TestDataOutPutModel>(totalCount, list == null ? new List<TestDataOutPutModel>() : list.ToList()));

            //int totalCount = 0;
            ////string sort = Utils.ObjectToStr(Request["sort"]);
            //string materielName = Utils.ObjectToStr(Request["materielName"]);
            //if (!string.IsNullOrEmpty(materielName))
            //    if (materielName.Contains("物料"))
            //        materielName = "";
            //List<RcBatchModel> list = bll.GetRcBatchList(new Models.CustomModels.TestDataQuertParaModel
            //{
            //    BatchCode = Utils.ObjectToStr(Request["batchCode"]),
            //    MaterielName = materielName,
            //    ISJSUAN = Utils.ObjectToStr(Request["isJsuan"]),
            //    ISRELATED = Utils.ObjectToStr(Request["jsRelated"]),
            //    PageSize = Utils.ObjToInt(Request["rows"], 15),
            //    PageIndex = Utils.ObjToInt(Request["page"], 1)
            //}, ref totalCount);

            //for(int i=0;i< list.Count;i++)
            //{
            //    list[i].STR_COLLECT_TIME = list[i].COLLECT_TIME.GetValueOrDefault().ToString("yyyy-MM-dd HH:mm:ss");
            //    if (list[i].BATCH_TYPE == "自采煤")
            //    {
            //        list[i].CSR = list[i].SELF_CSR;
            //        list[i].CRI = list[i].SELF_CRI;
            //        list[i].YX_VALUE = list[i].SELF_YANX_VALUE;
            //    }
            //}
            //return Json(new Pagination.PaginationResult<RcBatchModel>(totalCount, list.ToList()));
        }


        #region 关联
        [SupportFilter]
        public ActionResult Examine(string dataIds)
        {
            ViewBag.dataIds = dataIds;
            return View();
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Examine(TestDataOutPutModel model)
        {
            try
            {
                List<string> ids = model.dataIds.Split(',').ToList();
                if (ids.Count <= 0)
                {
                    return Json(JsonHandler.CreateMessage(0, "请至少选择一项"));
                }
                if (string.IsNullOrEmpty(model.XJLM_CODE))
                {
                    return Json(JsonHandler.CreateMessage(0, "请输入化验编号"));
                }
                //if (string.IsNullOrEmpty(model.XJLM_CODE))
                //{
                //    return Json(JsonHandler.CreateMessage(0, "请输入小焦炉焦炭编号"));
                //}
                //if (string.IsNullOrEmpty(model.BATCH_CODE))
                //{
                //    return Json(JsonHandler.CreateMessage(0, "请输入化验编号,并且先点击获取自采煤数据"));
                //}
                var info = bll.GetSelfSampingInfo(new TestDataQuertParaModel { TestCode = model.XJLM_CODE });
                if (string.IsNullOrEmpty(info.BATCH_CODE))
                    return Json(JsonHandler.CreateMessage(0, $"请确认此化验码{model.XJLM_CODE}是否已经通过审核"));
                if (!string.IsNullOrEmpty(model.XJLJT_CODE))
                {
                    info = bll.GetSelfSampingInfo(new TestDataQuertParaModel { TestCode = model.XJLJT_CODE });
                    if (string.IsNullOrEmpty(info.BATCH_CODE))
                        return Json(JsonHandler.CreateMessage(0, $"请确认此化验码{model.XJLM_CODE}是否已经通过审核"));
                }
                if (model.dataIds.Contains(info.BATCH_CODE))
                {
                    return Json(JsonHandler.CreateMessage(0, $"化验码所对应的批次，与需要关联的批次重复，不能自相关联，请检查已选批次"));
                }
                //model.BATCH_CODE = info.BATCH_CODE;// bllYx.GetBatchCode(model.TEST_CODE);
                //if (string.IsNullOrEmpty(model.YANX_VALUE))
                //    model.YANX_VALUE = "";
                //if (string.IsNullOrEmpty(model.CRI))
                //    return Json(JsonHandler.CreateMessage(0, "请先读取/输入CRI数据"));
                //if (string.IsNullOrEmpty(model.CSR))
                //    return Json(JsonHandler.CreateMessage(0, "请先读取/输入CSR数据"));
                //if (string.IsNullOrEmpty(model.YANX_VALUE))
                //    return Json(JsonHandler.CreateMessage(0, "请先读取/输入岩相数据"));
                if (bll.RelaterdData(model))
                {
                    return Json(JsonHandler.CreateMessage(1, "关联成功"));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    return Json(JsonHandler.CreateMessage(0, "关联失败" + ErrorCol));
                }
            }
            catch
            {
                string ErrorCol = errors.Error;
                return Json(JsonHandler.CreateMessage(0, "关联失败" + ErrorCol));
            }
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult UnExamine(TestDataOutPutModel model)
        {
            try
            {
                List<string> ids = model.dataIds.Split(',').ToList();
                if (ids.Count <= 0)
                {
                    return Json(JsonHandler.CreateMessage(0, "请至少选择一项"));
                }

                if (bll.UnRelaterdData(model))
                {
                    return Json(JsonHandler.CreateMessage(1, "关联取消成功"));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    return Json(JsonHandler.CreateMessage(0, "取消关联失败" + ErrorCol));
                }
            }
            catch
            {
                string ErrorCol = errors.Error;
                return Json(JsonHandler.CreateMessage(0, "取消关联失败" + ErrorCol));
            }
        }

        public ActionResult JieSuan(string dataIds)
        {
            ViewBag.dataIds = dataIds;
            return View();
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult JieSuanData(TestDataOutPutModel model)
        {
            try
            {
                List<string> ids = model.dataIds.Split(',').ToList();
                if (ids.Count <= 0)
                {
                    return Json(JsonHandler.CreateMessage(0, "请至少选择一项"));
                }
                if (bll.JieSuanData(model))
                {
                    return Json(JsonHandler.CreateMessage(1, "设置为结算状态成功"));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    return Json(JsonHandler.CreateMessage(0, "设置为结算状态失败" + ErrorCol));
                }
            }
            catch
            {
                string ErrorCol = errors.Error;
                return Json(JsonHandler.CreateMessage(0, "设置为结算状态失败" + ErrorCol));
            }
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult UnJieSuanData(TestDataOutPutModel model)
        {
            try
            {
                List<string> ids = model.dataIds.Split(',').ToList();
                if (ids.Count <= 0)
                {
                    return Json(JsonHandler.CreateMessage(0, "请至少选择一项"));
                }

                if (bll.UnJieSuanData(model))
                {
                    return Json(JsonHandler.CreateMessage(1, "结算状态取消成功"));
                }
                else
                {
                    string ErrorCol = errors.Error;
                    return Json(JsonHandler.CreateMessage(0, "结算状态取消失败" + ErrorCol));
                }
            }
            catch
            {
                string ErrorCol = errors.Error;
                return Json(JsonHandler.CreateMessage(0, "结算状态取消失败" + ErrorCol));
            }
        }

        #endregion

        #region 获取自采煤相关信息
        [HttpPost]
        [SupportFilter]
        public ActionResult Query(string id, string id2 = "")
        {
            TestDataOutPutModel info = new TestDataOutPutModel();
            TestDataOutPutModel model = new TestDataOutPutModel();
            if (!string.IsNullOrEmpty(id))
            {
                info = bll.GetSelfSampingInfo(new TestDataQuertParaModel { TestCode = id });
                if (info != null)
                {
                    if (!string.IsNullOrEmpty(info.CRI))
                        model.CRI = info.CRI;
                    if (!string.IsNullOrEmpty(info.CSR))
                        model.CSR = info.CSR;
                    if (!string.IsNullOrEmpty(info.YANX_VALUE))
                        model.YANX_VALUE = info.YANX_VALUE;
                }
                if (string.IsNullOrEmpty(info.BATCH_CODE))
                    return Json(JsonHandler.CreateMessage(0, $"请确认此化验码{id}是否已经通过审核"));
            }
            if (!string.IsNullOrEmpty(id2))
            {
                info = bll.GetSelfSampingInfo(new TestDataQuertParaModel { TestCode = id2 });
                if (info != null)
                {
                    if (!string.IsNullOrEmpty(info.CRI))
                        model.CRI = info.CRI;
                    if (!string.IsNullOrEmpty(info.CSR))
                        model.CSR = info.CSR;
                    if (!string.IsNullOrEmpty(info.YANX_VALUE))
                        model.YANX_VALUE = info.YANX_VALUE;
                }
                if (string.IsNullOrEmpty(info.BATCH_CODE))
                    return Json(JsonHandler.CreateMessage(0, $"请确认此化验码{id2}是否已经通过审核"));
            }
            //if (string.IsNullOrEmpty(model.CRI) || string.IsNullOrEmpty(model.CSR))
            //    return Json(JsonHandler.CreateMessage(0, "请确认此化验数据是否已经通过审核"));
            return Json(model);
        }
        #endregion
    }
}
